package com.amt.olcacon.utils;

import com.amt.olcacon.entity.ExchangeEntity;
import com.amt.olcacon.entity.ProcessEntity;
import org.openlca.core.model.Exchange;
import org.openlca.core.model.FlowPropertyFactor;
import org.openlca.core.model.Process;
import org.openlca.core.model.ProductSystem;

import java.util.List;

/**
 * openLCA工具类
 */
public class OlcaUtils {

    public static void cleanNoneFile(ProductSystem productSystem) {
        List<Exchange> exchanges = productSystem.referenceProcess.exchanges;
        exchanges.forEach(exchange -> {
            exchange.flowPropertyFactor = null;
            exchange.unit = null;
            exchange.flow.category = null;
            exchange.flow.referenceFlowProperty = null;
            exchange.flow.flowPropertyFactors.forEach(flowPropertyFactor -> {
                flowPropertyFactor.flowProperty = null;
            });
        });
        productSystem.referenceProcess.location = null;
        productSystem.referenceProcess.quantitativeReference.flow.referenceFlowProperty = null;

        List<FlowPropertyFactor> flowPropertyFactors = productSystem.referenceProcess.quantitativeReference.flow.flowPropertyFactors;
        flowPropertyFactors.forEach(flowPropertyFactor -> {
            flowPropertyFactor.flowProperty = null;
        });
        productSystem.referenceProcess.exchangeDqSystem = null;
        productSystem.referenceProcess.documentation = null;
        productSystem.referenceProcess.quantitativeReference.flowPropertyFactor = null;
        productSystem.referenceProcess.quantitativeReference.unit = null;
        productSystem.targetFlowPropertyFactor = null;
    }


    public static ExchangeEntity buildExchangeEntity(Exchange exchange, org.openlca.core.model.Process sysProc) {
        ExchangeEntity ee = new ExchangeEntity();
        ee.setId(exchange.id);
        ee.setFOwner(sysProc.id);
        ee.setInternalId(exchange.internalId);
        ee.setFFlow(exchange.flow != null ? exchange.flow.id : 0L);
        ee.setFUnit(exchange.unit != null ? exchange.unit.id : 0L);
        ee.setIsInput(exchange.isInput);
        ee.setFFlowPropertyFactor(exchange.flowPropertyFactor != null ? exchange.flowPropertyFactor.id : 0L);
        ee.setResultingAmountValue(exchange.amount);
        ee.setResultingAmountFormula(exchange.formula);
        ee.setAvoidedProduct(exchange.isAvoided);
        ee.setFDefaultProvider(exchange.defaultProviderId);
        ee.setFLocation(exchange.location != null ? exchange.location.id : 0L);
        ee.setDescription(exchange.description);
        ee.setCostValue(exchange.costs);
        ee.setCostFormula(exchange.costFormula);
        ee.setFCurrency(exchange.currency != null ? exchange.currency.id : 0L);
        ee.setDistributionType(exchange.uncertainty != null ? exchange.uncertainty.distributionType.ordinal() : 0);
        ee.setParameter1Value(exchange.uncertainty != null ? exchange.uncertainty.parameter1 : 0);
        ee.setParameter2Value(exchange.uncertainty != null ? exchange.uncertainty.parameter2 : 0);
        ee.setParameter3Value(exchange.uncertainty != null ? exchange.uncertainty.parameter3 : 0);
        ee.setDqEntry(exchange.dqEntry);
        ee.setBaseUncertainty(exchange.baseUncertainty);
        return ee;
    }

    public static ProcessEntity buildProcessEntity(Process sysProc) {
        ProcessEntity pe = new ProcessEntity();
        pe.setId(sysProc.id);
        pe.setRefId(sysProc.refId);
        pe.setName(sysProc.name);
        pe.setVersion(sysProc.version);
        pe.setLastChange(sysProc.lastChange);
        pe.setFCategory(10086L);
        pe.setTags(sysProc.tags);
        pe.setLibrary(sysProc.library);
        pe.setDescription(sysProc.description);
        pe.setOtherProperties(sysProc.otherProperties);
        pe.setProcessType(sysProc.processType.name());
        pe.setDefaultAllocationMethod(sysProc.defaultAllocationMethod.name());
        pe.setInfrastructureProcess(sysProc.infrastructureProcess);
        pe.setFQuantitativeReference(sysProc.quantitativeReference != null ? sysProc.quantitativeReference.id : 0L);
        pe.setFLocation(sysProc.location != null ? sysProc.location.id : 0L);
        pe.setFProcessDoc(sysProc.documentation != null ? sysProc.documentation.id : 0L);
        pe.setFDqSystem(sysProc.dqSystem != null ? sysProc.dqSystem.id : 0L);
        pe.setDqEntry(sysProc.dqEntry);
        pe.setFExchangeDqSystem(sysProc.exchangeDqSystem != null ? sysProc.exchangeDqSystem.id : 0L);
        pe.setFSocialDqSystem(sysProc.socialDqSystem != null ? sysProc.socialDqSystem.id : 0L);
        pe.setLastInternalId(sysProc.lastInternalId);
        return pe;
    }

}
